package com.briup.user.controller;

import com.briup.common.response.Result;
import com.briup.user.domain.pojo.Address;
import com.briup.user.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/address")
public class AddressController {
    @Autowired
    private AddressService addressService;

    /**
     * 查询当前用户下所有的收货地址
     */
    @GetMapping
    public Result<List<Address>> getAllAddress(){
        List<Address> addresses = addressService.getAll();
        return Result.success(addresses);
    }

    /**
     * 添加收货地址
     */
    @PostMapping
    public Result addAddress(@RequestBody Address address){
        addressService.addAddress(address);
        return Result.success();
    }


    /**
     * 修改收货地址
     */
    @PutMapping
    public Result updateAddress(@RequestBody Address address){
        addressService.updateAddress(address);
        return Result.success();
    }

    /**
     * 删除收货地址
     */
    @DeleteMapping("/{id}")
    public Result deleteAddress(@PathVariable Integer id){
        addressService.deleteAddress(id);
        return Result.success();
    }


    /**
     * 设置默认收货地址
     */
    @PutMapping("/set/{id}")
    public Result setDefaultAddress(@PathVariable Integer id){
        addressService.setDefaultAddress(id);
        return Result.success();
    }


    /**
     * 取消默认收货地址
     */
    @PutMapping("/cancel/{id}")
    public Result cancelDefaultAddress(@PathVariable Integer id){
        addressService.cancelDefaultAddress(id);
        return Result.success();
    }
}
